package handle.data;

import save.data.User;
import utils.DataBaseUtil;
import utils.MD5Util;
import javax.naming.NamingException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import java.io.IOException;
import java.sql.*;

@WebServlet(name = "login", value = "/login")
public class login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Connection con = null;
        PreparedStatement preparedStatement = null;
        String logName = request.getParameter("username").trim();
        String lohPassword = request.getParameter("password").trim();
        lohPassword = MD5Util.encrypt(lohPassword);

        HttpSession session=request.getSession(true);
        User loginBean=null;
        try{  loginBean=(User) session.getAttribute("loginBean");
            if(loginBean==null){
                loginBean=new User();  //创建新的数据模型 。
                session.setAttribute("loginBean",loginBean);
                loginBean=(User) session.getAttribute("loginBean");
            }

        }
        catch(Exception ee){
            loginBean=new User();
            session.setAttribute("loginBean",loginBean);

        }
        ResultSet rs = null;
        //判断用户名和密码是否不为空
        boolean boo = (logName.length() > 0) && (lohPassword.length() > 0);
        if (boo) {
            try {

                con = DataBaseUtil.getConnection();
                String sql = "select * from user where username = ? and password=?";
                preparedStatement = con.prepareStatement(sql);
                preparedStatement.setString(1, logName);
                preparedStatement.setString(2, lohPassword);
                rs = preparedStatement.executeQuery();
                boolean m = rs.next();
                int userId ;
                if (m) {
                    //登录成功 转发到首页
                    loginBean.setLoginBack("登录成功");
                    loginBean.setUserId(rs.getInt("userId"));
                    loginBean.setUsername(rs.getString("username"));
                    RequestDispatcher dispatcher =
                            request.getRequestDispatcher("index");//转发
                    dispatcher.forward(request, response);
                } else {

                    loginBean.setLoginBack("您输入的用户名不存在，或密码不般配");
                    //调用登录失败跳转到login.jsp: 您输入的用户名不存在，或密码不般配
                    response.sendRedirect("login.jsp");


                }

            } catch (SQLException e) {
                System.out.println("e = " + e);
            } catch (NamingException e) {
                System.out.println("没有设置连接池" + e);
            } finally {
                try {
                    DataBaseUtil.close(rs, null, con, null);//连接返回连接池。
                } catch (Exception e) {
                    System.out.println("e = " + e);
                }
            }
        } else {
            loginBean.setLoginBack("请输入用户名和密码");
            //请输入用户名和密码
            response.sendRedirect("login.jsp");

        }
    }

}
